package com.hmall.search.listener;

import com.hmall.feign.pojo.Item;
import com.hmall.search.service.impl.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ItemListener {
    @Autowired
    private SearchService searchService;

    /**
     * 上架
     * 创建索引
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "up.queue"),
            exchange = @Exchange(name = "item-exchange",type = ExchangeTypes.TOPIC),
            key = "item.up"
    ))
    public void receiveUpMsg(String itemId){
        log.info("接收到上架消息，msg={}",itemId);
        searchService.createIndex(itemId);
    }


    /**
     * 下架
     * 删除索引
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "down.queue"),
            exchange = @Exchange(name = "item-exchange",type = ExchangeTypes.TOPIC),
            key = "item.down"
    ))
    public void receiveDownMsg(String itemId){
        log.info("接收到下架消息，msg={}",itemId);
        searchService.removeIndex(itemId);
    }
    /**
     * 删除货物
     * 删除索引
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delete.queue"),
            exchange = @Exchange(name = "item-exchange",type = ExchangeTypes.TOPIC),
            key = "item.delete"
    ))
    public void receiveDownMsg1(String itemId){
        log.info("接收到删除消息，msg={}",itemId);
        searchService.removeIndex(itemId);
    }
    /**
     * 新增货物
     * 添加索引
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "add.queue"),
            exchange = @Exchange(name = "item-exchange",type = ExchangeTypes.TOPIC),
            key = "item.add"
    ))
    public void receiveDownMsg2(String itemId){
        log.info("接收到新增消息，msg={}",itemId);
        searchService.createIndex(itemId);
    }
    /**
     * 更新货物
     * 删除索引
     * 添加索引
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "update.queue"),
            exchange = @Exchange(name = "item-exchange",type = ExchangeTypes.TOPIC),
            key = "item.update"
    ))
    public void receiveDownMsg3(String itemId){
        log.info("接收到更新消息，msg={}",itemId);
        searchService.removeIndex(itemId);
        searchService.createIndex(itemId);
    }

}
